<template>
	<view v-if="user.frentLink!==5&&user!==''">
		<navbox title="发布动态"></navbox>
		<view style="margin-top: 190rpx;background-color: #f9f9f9;height: 100%;" >
			<view style="height: 15rpx;"></view>
			<u--textarea v-model="freDesc" placeholder="说点什么吧..." border="bottom"></u--textarea>
			<view style="display: flex;align-items: center;margin-top: 20rpx;background-color: #fff;">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="9" width="120" height="120" sizeType="compressed">
				</u-upload>
			</view>
			<view style="background-color: #fff;margin-top: 20rpx">
				<u-cell-group style="">
					<u-cell size="large" title="权限" :value="status[0]" isLink @click="show=true"></u-cell>
				</u-cell-group>
			</view>
			<view style="margin-top: 60rpx;">
				<u-button type="primary" text="确定" @click="uploadpic()"></u-button>
			</view>

			<u-picker :show="show" :columns="columns" @cancel="show=false" :value="status"  @confirm="confirm()"></u-picker>
		</view>
	</view>
</template>

<script>
	import navbox from '../components/navbox/navbox.vue'
	export default {
		data() {
			return {
				pre: "",
				fileList1: [],
				show: false,
				columns: [
					['公共', '个人']
				],
				data: [],
				user: uni.getStorageSync("user"),
				freDesc: '',
				status:''
			}
		},
		components: {
			"navbox": navbox
		},
		methods: {
			confirm: function(e) {
				this.status = e.value
				console.log(this.status)
				this.show=false
			},
			async uploadpic() {
				const frent = {
					userId: this.user.id,
					userName: this.user.nickName,
					picUrl: this.user.picUrl,
					freDesc: this.freDesc,
					status: this.status[0]==='公共'?1:0,
					urls:this.data
				}
				if(this.status===null||this.status===''){
					return uni.showToast({
						title:"请选择权限",
						icon:'none'
					})
				}
				if(this.freDesc===null||this.freDesc===''){
					return uni.showToast({
						title:"请填写内容",
						icon:'none'
					})
				}
				const res = await this.$myRuquest({
					url: '/wexinLogin/pic/NewloadPics',
					method: 'post',
					data:frent
				})
				uni.switchTab({
					url:"/pages/friends/friends"
				})				
			},
			// 删除图片
			deletePic(event) {
				this.data.splice(event.index, 1)
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					console.log(123)
					let a = uni.uploadFile({
						url: this.$baseurl + '/wexinLogin/pictures/uploadFrent?userId=' + this.user.id, // 仅为示/uploadFrent?例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							'token': uni.getStorageSync('token')
						},
						success: (res) => {
                            const da = JSON.parse(res.data)
							console.log(da)
                            this.data.push(da.data.url)
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}

	}
</script>

<style>

</style>
